package com.xy.smartsms.plugincmcc;

import android.content.Context;
import android.text.TextUtils;

import com.xy.smartsms.data.ISmsPlugin;
import com.xy.smartsms.data.SmsItem;
import com.xy.smartsms.util.JsonUtil;
import com.xy.smartsms.plugincmcc.utils.SysparaUtil;

import org.json.JSONObject;

import java.util.List;

public class CMCCBeforeParseUtil extends Thread {

    private Context mContext = null;
    private static final Object lock = new Object();
    private static boolean stopParse = false;
    private static boolean parseIng = false;
    private String phoneNum;
    private ISmsPlugin mSmsPlugin = null;
    private long mMaxTime = 0L;
    private static long threadId = 0;
    List<SmsItem> mListItem = null;

    public static void setStop() {
        synchronized (lock) {
            if (parseIng) {
                stopParse = true;
            }
        }
    }
    
    public static void executeBeforeParse(Context context,
                                          ISmsPlugin plugin,
                                          String phoneNum,
                                          List<SmsItem> list,
                                          long maxTime) {
        CMCCBeforeParseUtil beforeParseThread = new CMCCBeforeParseUtil(context, plugin, phoneNum, list, maxTime);
        beforeParseThread.setPriority(Thread.MIN_PRIORITY);
        beforeParseThread.start();
    }

    private CMCCBeforeParseUtil(Context context,
                                ISmsPlugin plugin,
                                String phoneNum,
                                List<SmsItem> list,
                                long maxTime) {
        this.mContext = context;
        this.phoneNum = phoneNum;
        this.mListItem = list;
        mSmsPlugin = plugin;
        mMaxTime = maxTime;
        this.setName("cmcc_before_parse_thread");
    }

    private void execute() throws InterruptedException {
        // history sms parse from new to old
        if (stopParse || threadId != Thread.currentThread().getId()) {
            return;
        }

        int size = mListItem.size();
        for (int i = 0; i < size; i++) {
            SmsItem item = mListItem.get(i);
            long smsReceiveTime = item.getReceiveTime();
            if (smsReceiveTime < mMaxTime) {
                mMaxTime = smsReceiveTime; // hisory sms parse from new to old
            }

            mSmsPlugin.parseCardSync(ISmsPlugin.TYPE_RICH, item);
            if (TextUtils.isEmpty(phoneNum)) {
                SysparaUtil.savelong(mContext, "BEFORE_HAND_PARSE_SMS_TIME", mMaxTime);
            } else {
                CMCCBeforeInfo.getInstance(mContext).saveOrUpdateObject(item.getPhone(), mMaxTime, null);
            }

            sleep(1);

            if (stopParse || threadId != Thread.currentThread().getId()) {
                break;
            }
        }
    }

    @Override
    public void run() {
        try {
            android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
            Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
            synchronized (lock) {
                if (parseIng) {
                    stopParse = true;
                }
            }

            synchronized (lock) {
                if (stopParse) {
                    return;
                }
                parseIng = true;
                stopParse = false;
                threadId = Thread.currentThread().getId();
            }
            execute();
        } catch (Throwable e) {

        } finally {
            synchronized (lock) {
                parseIng = false;
                stopParse = false;
                threadId = 0;
            }

        }
    }


    public static long getMaxTime(Context context, String phoneNum) {
        long maxTime = 0;
        if (TextUtils.isEmpty(phoneNum)) {
            maxTime = SysparaUtil.getlong(context, "BEFORE_HAND_PARSE_SMS_TIME", 0);
        } else {
            JSONObject jsObj = CMCCBeforeInfo.getInstance(context).findObjectByPhone(phoneNum);
            if (jsObj != null) {
                Object obj = JsonUtil.getValFromJsonObject(jsObj, "maxReceiveTime");
                if (obj != null) {
                    maxTime = Long.valueOf(obj.toString());
                }
            }
        }

        if (maxTime == 0) {
            maxTime = System.currentTimeMillis() + Integer.MAX_VALUE;
        }

        return maxTime;
    }
}
